package com.googlecode.bip.entity.impl;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
 *
 */
public class EntityHelper
{
    private EntityHelper()
    {
        //All static
    }

//    public static Iterable<Timestamp> getIterable(
//        final Date from,
//        final Date to
//        )
//    {
//        return new Iterable<Timestamp>()
//        {
//            @Override
//            public Iterator<Timestamp> iterator()
//            {
//                return getIterator( from, to );
//            }
//        };
//    }
//
//    public static Iterator<Timestamp> getIterator(
//        final Date from,
//        final Date to
//        )
//    {
//        final Calendar fromCalendar   = new GregorianCalendar();;
//        final Calendar toCalendar     = new GregorianCalendar();;
//
//        fromCalendar.setTime( EntityHelper.getNormalizedTimestamp( from ) );
//        toCalendar.setTime( EntityHelper.getNormalizedTimestamp( to ) );
//
//        return new Iterator<Timestamp>()
//        {
//            Calendar c = Calendar.class.cast( fromCalendar.clone() );
//
//            @Override
//            public boolean hasNext()
//            {
//                return c.compareTo( toCalendar ) <= 0;
//            }
//
//            @Override
//            public Timestamp next()
//            {
//                Timestamp ts = new Timestamp( c.getTimeInMillis() );
//
//                c.add( Calendar.DAY_OF_MONTH, 1 );
//
//                //return EntityHelper.getNormalizedTimestamp( ts );
//                return ts;
//            }
//
//            @Override
//            public void remove()
//            {
//                throw new UnsupportedOperationException();
//            }
//        };
//    }

    /**
     * Normalized {@link Calendar}
     *
     * @param calendar {@link Calendar} to normalize
     * @return Giving {@link Calendar} after normalization
     */
    public static Calendar getNormalizedCalendar(
            final Calendar calendar
            )
    {
        calendar.set( Calendar.MILLISECOND, 0 );   // ignore milliseconds
        calendar.set( Calendar.SECOND, 0 );        // ignore seconds
        calendar.set( Calendar.MINUTE, 0 );        // ignore minutes
        calendar.set( Calendar.HOUR, 0 );          // ignore hours
        calendar.set( Calendar.AM_PM, 0 );         // ignore AM/PM (1 value per day)

        return calendar;
    }

    /**
     * Create normalized Timestamp from initialized Calendar
     *
     * @param calendar initialized Calendar
     * @return normalized Timestamp
     */
    public static Timestamp getNormalizedTimestamp(
            final Calendar calendar
            )
    {
        return new Timestamp( getNormalizedCalendar( calendar ).getTimeInMillis() );
    }

    /**
     * Create normalized Timestamp from initialized Calendar
     *
     * @param date (could be null)
     * @param userTimeZone (used to initialized date from server date + user timezone)
     * @return normalized Timestamp
     */
    public static Timestamp getNormalizedTimestamp(
            final Date      date,
            final TimeZone  userTimeZone
            )
    {
        Calendar c = new GregorianCalendar();

        if( date != null ) {
            c.setTime( date );
            }
        // else use current date/time
        // TODO: warning this is server date/time (not user !!!)
        // should be user date/time TODO use userTimeZone

        return getNormalizedTimestamp( c );
    }

    /**
     * Create normalized Timestamp from initialized Calendar
     *
     * @param date a valid Date/Timestamp that must be normalized
     * @return normalized Timestamp
     */
    public static Timestamp getNormalizedTimestamp(
            final Date date
            )
    {
        Calendar c = new GregorianCalendar();

        if( date == null ) {
            throw new NullPointerException( "date is null" );
            }

        c.setTime( date );

        return getNormalizedTimestamp( c );
    }

    /**
     * Check is giving String contain only alpha numerics characters,
     * no white spaces
     * @param str
     * @return
     */
    public static boolean isValidAlphanumericNoWhiteSpaces( final String str )
    {
        final int l = str.length();

        for(int i = 0; i<l; i++ ) {
            if( !Character.isLetterOrDigit( str.charAt( i ) ) ) {
                return false;
            }
        }

        return true;
    }

    /**
     *
     * @param s
     * @return
     */
    public static String getNotEmptyValue( final String s )
    {
        if( s != null ) {
            final String nStr = s.trim();

            if( nStr.length() != 0 ) {
                return nStr;
                }
            }

        return null;
    }

}
